package com.pj.project.wx.wx_card;


import cn.dev33.satoken.annotation.SaIgnore;
import com.pj.project.sys_user.SysUser;
import com.pj.project.sys_user.SysUserMapper;
import com.pj.project.sys_user.SysUserService;
import com.pj.utils.sg.AjaxJson;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpMemberCardService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.card.membercard.*;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.open.api.WxOpenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@SaIgnore
@RestController
@RequestMapping("/card/")
@Slf4j
public class WxMpCardController {
    @Autowired
    WxMpService wxMpService;
    @Autowired
    WxOpenService wxOpenService;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    WxCardMapperw wxCardMapper;
    @Autowired
    WxCardService wxCardService;
    @Autowired
    SysUserService sysUserService;

    //  用户领卡回调 /*, produces = { "application/json;charset=utf-8" }*/
    @ResponseBody
    @RequestMapping("membercard/activate")
    public Object activate(WxMpXmlMessage wxMpXmlMessage){

        String unionid;
        try {
            WxMpUser wxMpUser = wxMpService.getUserService().userInfo(wxMpXmlMessage.getFromUser());
            log.info("使用公众号openid查询用户信息以拿到unionid"+wxMpUser.toString());
            unionid = wxMpUser.getUnionId();
            wxMpXmlMessage.setUnionId(unionid);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }

        //根据code序列号查到该卡的用户信息
        SysUser byOpenId = sysUserMapper.getByCardCode(wxMpXmlMessage.getUserCardCode());
        //库中无记录 则记录
        if(byOpenId==null){
            log.info("会员卡入库->");
            WxCard wxCard = wxCardService.InIt(wxMpXmlMessage);
            byOpenId = sysUserMapper.getByCardCode(wxMpXmlMessage.getUserCardCode());
        }else{  //有记录 则更新时间
            log.info("有会员卡记录则更新时间->");
            WxCard wxCard = new WxCard();
            wxCard.setUserCardCode(wxMpXmlMessage.getUserCardCode());
            wxCardService.updateTime(wxCard);
        }

        log.info("byOpenId->"+byOpenId);


        if(wxMpXmlMessage.getIsRestoreMemberCard().equals("1")){    //若为1则是找回操作 ,在库中查找之前的记录将其同步
            try {
                //向微信发起会员卡更新事件
                log.info("向微信发起会员卡更新事件：->");
                WxMpMemberCardUpdateMessage wxMpMemberCardActivatedMessage = new WxMpMemberCardUpdateMessage();
                wxMpMemberCardActivatedMessage.setCode(wxMpXmlMessage.getUserCardCode());   //卡券Code码
                wxMpMemberCardActivatedMessage.setBonus(byOpenId.getTotalPoints());   //同步所有积分
                wxMpMemberCardActivatedMessage.setRecordBonus("再次使用,同步您之前的积分");  //变动记录内容
                WxMpMemberCardUpdateResult wxMpMemberCardUpdateResult = wxMpService.getMemberCardService().updateUserMemberCard(wxMpMemberCardActivatedMessage);
                log.info("微信返回结果：->"+wxMpMemberCardUpdateResult);
            }catch (Exception e){
                log.error(e.getMessage());
            }
        }else{  //  若为0则是第一次激活 将卡券信息入库管理
            try {
                log.info("微信发送会员卡激活操作：->");
                //通过接口发起会员卡激活操作
                WxMpMemberCardActivatedMessage wxMpMemberCardActivatedMessage = new WxMpMemberCardActivatedMessage();
                wxMpMemberCardActivatedMessage.setCode(wxMpXmlMessage.getUserCardCode());
                wxMpMemberCardActivatedMessage.setMembershipNumber(wxMpXmlMessage.getUserCardCode()); //会员卡编号，由开发者填入，作为序列号显示在用户的卡包里。可与Code码保持等值。
                wxMpMemberCardActivatedMessage.setInitBonus(byOpenId.getTotalPoints()); //将用户在积分系统的积分同步到微信
                String s = wxMpService.getMemberCardService().activateMemberCard(wxMpMemberCardActivatedMessage);
                log.info("微信返回结果：->"+s);
            }catch (Exception e){
                log.error(e.getMessage());
            }
        }
        return "";

    }

    // 会员卡内容更新事件回调 微信回调
    @ResponseBody
//    @RequestMapping("membercard/updateuser")
    public Object updateuser(WxMpXmlMessage wxMpXmlMessage){
        // 积分变动
        sysUserService.updateuser(wxMpXmlMessage);

        return "";

    }


    // 获取开卡参数
    @ResponseBody
    @RequestMapping("membercard/getOpenCardParam")
    public AjaxJson getOpenCardParam(WxMpXmlMessage wxMpXmlMessage) throws Exception{
        WxMpMemberCardService memberCardService = wxMpService.getMemberCardService();
        ActivatePluginParam activatePluginParam = memberCardService.getActivatePluginParam("pq1on6GzI5XDw2k46PCxmF1n_IRY","");

        return AjaxJson.getSuccessData(activatePluginParam);

    }







}
